<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="#">新闻管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>新闻列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--  输入框组-->
    <div>
      <br/>
      <span>新闻名称 &nbsp;&nbsp;</span>
      <el-input
          placeholder="请输入新闻名称"
          prefix-icon="el-icon-search"
          v-model="name" class="inputMax" >
      </el-input>
      <span>&nbsp;&nbsp;新闻类型&nbsp;&nbsp;</span>
      <el-select v-model="articleType" placeholder="请选择" class="inputMax">
        <el-option
            v-for="item in articleTypes"
            :key="item.id"
            :label="item.name"
            :value="item.name">
        </el-option>
      </el-select>
      &nbsp;&nbsp;
      <el-button type="primary" icon="el-icon-search" @click="articleSearch" ><span>查询</span></el-button>
      <el-button type="success" icon="el-icon-plus" @click="toAddArticle">添加</el-button>
      <br/>
      <br/>
    </div>
    <el-table
        :data="articles"
        border
        style="width: 100%">
      <el-table-column
          prop="id"
          label="ID"
          width="50">
      </el-table-column>
      <el-table-column
          prop="name"
          label="新闻名称"
          width="200">
      </el-table-column>
      <el-table-column
          prop="type"
          label="新闻类型">
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="发布时间"
          width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | DateFormat }}</span>
        </template>
      </el-table-column>
      <el-table-column
          prop="status"
          label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status==0">未发布</span>
          <span v-else-if="scope.row.status==1">审核中</span>
          <span v-else>已发布</span>
        </template>
      </el-table-column>
      <el-table-column
          prop="content"
          label="新闻内容"
          show-overflow-tooltip
          width="400">
      </el-table-column>
      <el-table-column
          fixed="right"
          width="270"
          label="操作">
        <template slot-scope="scope">
          <el-button type="success" icon="el-icon-check" size="mini" :disabled="scope.row.status!=0" @click="updateArt(scope.row)">发布</el-button>
          <el-button type="primary" icon="el-icon-edit" size="mini" :disabled="scope.row.status!=0" @click="toUpdateArticle(scope.row.id)" >编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="open(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        background
        layout="prev, pager, next"
        :page-size="pageSize"
        :total="total">
    </el-pagination>
  </div>
</template>

<script>
import {articleDelete, articleList, articleTypeList,articleUpdate} from "@/api";

export default {
  name: "ArticleList",
  components: {
  },
  data() {
    return {
      name:'',
      articleTypes:[],
      articleType: '',
      articles:[],
      total: 0,
      pageSize: 0,
      c_id: sessionStorage.getItem('cid'),
    }
  },

  created() {
    articleTypeList().then((result)=>{
      this.articleTypes = result.data;
      this.articleTypes.unshift({id:0,name:''})
    });
    let article = {c_id: this.c_id};
    articleList(article).then((result)=>{
      this.articles = result.data.list;
      this.total = result.data.total;
      this.pageSize = result.data.pagesize;
    })
  },

  methods: {
    articleSearch(){
      let article = {
        name: this.name,
        type: this.articleType,
        c_id: this.c_id,
      };
      articleList(article).then((result)=>{
        this.articles = result.data.list;
        this.total = result.data.total;
        this.pageSize = result.data.pagesize;
      });
    },

    toAddArticle(){
      this.$router.push('/mainCom/articleAdd');
    },

    toUpdateArticle(id){
      this.$router.push({
        path: '/mainCom/articleAdd',
        query: {
          id: id,
        }
      })
    },
    updateArt(article){
      article.status=1;
      articleUpdate(article);
    },
    open(id) {
      this.$confirm('此操作将永久删除该新闻, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let article = {
          id: id,
          c_id: this.c_id,
        }
        articleDelete(article).then(()=>{
          let article1 = {c_id: this.c_id};
          articleList(article1).then((result)=>{
            this.articles = result.data.list;
            this.total = result.data.total;
            this.pageSize = result.data.pagesize;
          });
        });
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  }
}
</script>

<style scoped>
.inputMax{
  width: 200px;
}
</style>
